import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class Main {
    public static void main(String[] args) throws IOException, URISyntaxException, InterruptedException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://100.101.23.23:9000");  // 配置集群ip和端口
        FileSystem fileSystem = FileSystem.get(new URI("hdfs://100.101.23.23:9000"), conf, "hadoop");
        Path path = new Path("/home/haha_03");
        boolean exists = fileSystem.exists(path);
        if(exists){
            System.out.println("文件存在，请删除！");
            fileSystem.delete(path,false);
            boolean checkExists = fileSystem.exists(path);
            if(!checkExists){
                System.out.println("文件删除成功！");
            }
        }else{
            System.out.println("文件不存在");
//            Path scrPath = new Path("/home/haha_03");  // linux
            Path scrPath = new Path("C:\\Users\\w00406273\\Desktop\\haha_03");  // windows
            fileSystem.copyFromLocalFile(scrPath,path);
            boolean checkExists = fileSystem.exists(path);
            if(checkExists){
                System.out.println("文件上传成功！");
            }
        }


    }
}
